import util from "../../../../utils/util";
const app = getApp();
Page({
  data: {
    city: [],
    cityId: "",
    showCity: true
  },

  onLoad: function (options) {
    util.getUrl(app.globalData.url).then(() => {
      this.getCityInfo();
      this.type = options.type;
    });
  },

  getCityInfo() {
    let data = {
      action: "cut_off",
      category: this.type,
      type: "city"
    };
    util.request("zhuanti.php", data, "POST").then(res => {
      if (res.data.state == 1) {
        let cityKey = [];
        for (let i = 65; i <= 90; i++)
          cityKey.push({
            key: String.fromCodePoint(i),
            list: []
          })
        cityKey.forEach(item => item.list = res.data.data.city[item.key]);
        this.setData({
          city: cityKey,
        });
      }
    });

  },

  scrollTo(e) {
    let id = util.getValue("id", e);
    this.setData({
      cityId: id,
    });
  },

  // 输入事件
  inputTap(e) {
    let keywords = e.detail.value;
    this.setData({
      keywords,
      showCity: false
    })
    util.cityList(keywords).then((res) => {
      this.setData({
        cityRes: res.cityRes,
      })
    })
  },

  // 输入框回车搜索事件
  confirmTap(e) {
    let keywords = e.detail.value;
    this.setData({
      keywords,
      showCity: false
    })
    util.cityList(keywords).then((res) => {
      this.setData({
        cityRes: res.cityRes,
      })
    })
  },

  // 点击搜索
  searchCity() {
    this.setData({
      // keywords,
      showCity: false
    })
    util.cityList(this.data.keywords).then((res) => {
      this.setData({
        cityRes: res.cityRes,
      })
    })
  },

  // 输入框删除icon事件
  delTap() {
    this.setData({
      keywords: "",
      showCity: true,
    })
  },
  
  chouseCity(e) {
    let citycode = util.getValue("citycode", e);
    let cityname = util.getValue("cityname", e);
    let curPages = getCurrentPages();
    curPages[curPages.length - 2].setData({
      citycode,
      cityname
    })

    wx.navigateBack({
      delta: 1
    });
  },

  onShareAppMessage: function () {

  }
})